home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / swtools / mipsABI / examples / sup / supmsg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  14.6 KB  |  623 lines

  1. /*
  2.  * Copyright (c) 1992 Carnegie Mellon University
  3.  * All Rights Reserved.
  4.  * 
  5.  * Permission to use, copy, modify and distribute this software and its
  6.  * documentation is hereby granted, provided that both the copyright
  7.  * notice and this permission notice appear in all copies of the
  8.  * software, derivative works or modified versions, and any portions
  9.  * thereof, and that both notices appear in supporting documentation.
  10.  *
  11.  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  12.  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  13.  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  14.  *
  15.  * Carnegie Mellon requests users of this software to return to
  16.  *
  17.  *  Software Distribution Coordinator  or  Software_Distribution@CS.CMU.EDU
  18.  *  School of Computer Science
  19.  *  Carnegie Mellon University
  20.  *  Pittsburgh PA 15213-3890
  21.  *
  22.  * any improvements or extensions that they make and grant Carnegie Mellon
  23.  * the rights to redistribute these changes.
  24.  */
  25. /*
  26.  **********************************************************************
  27.  * HISTORY
  28.  *
  29.  * 7-July-93  Nate Williams at Montana State University
  30.  *    Modified SUP to use gzip based compression when sending files
  31.  *    across the network to save BandWidth
  32.  *
  33.  * $Log: supmsg.c,v $
  34.  * Revision 1.2  1993/08/04  17:46:23  brezak
  35.  * Changes from nate for gzip'ed sup
  36.  *
  37.  * Revision 1.1.1.1  1993/05/21  14:52:19  cgd
  38.  * initial import of CMU's SUP to NetBSD
  39.  *
  40.  * Revision 2.4  92/09/09  22:05:17  mrt
  41.  *     Moved PFI definition under __STDC__ conditional since it
  42.  *     is already defined in libc.h in this case.
  43.  *     [92/09/01            mrt]
  44.  * 
  45.  * Revision 2.3  92/08/11  12:08:12  mrt
  46.  *     Added copyright
  47.  *     [92/08/10            mrt]
  48.  *     Brad's changes: Delinted, Incorporated updated variable 
  49.  *     argument list usage from old msgxfer.c
  50.  *     [92/07/24            mrt]
  51.  * 
  52.  * Revision 2.2  89/08/23  15:02:56  gm0w
  53.  *     Created from separate message modules.
  54.  *     [89/08/14            gm0w]
  55.  * 
  56.  **********************************************************************
  57.  */
  58. #include <stdio.h>
  59. #include <sys/types.h>
  60. #include <sys/stat.h>
  61. #if __STDC__
  62. #include <stdarg.h>
  63. #else
  64. #include <varargs.h>
  65. #endif
  66. #include <libc.h>
  67. #include <c.h>
  68. #include "sup.h"
  69. #define MSGSUBR
  70. #define MSGFILE
  71. #include "supmsg.h"
  72.  
  73. /*
  74.  * signon message
  75.  */
  76. extern int    pgmver;            /* program version of partner */
  77. extern int    pgmversion;        /* my program version */
  78. extern char    *scmver;        /* scm version of partner */
  79. extern int    fspid;            /* process id of fileserver */
  80.  
  81. int msgsignon ()
  82. {
  83.     register int x;
  84.  
  85.     if (server) {
  86.         x = readmsg (MSGSIGNON);
  87.         if (x == SCMOK)  x = readint (&protver);
  88.         if (x == SCMOK)  x = readint (&pgmver);
  89.         if (x == SCMOK)  x = readstring (&scmver);
  90.         if (x == SCMOK)  x = readmend ();
  91.     } else {
  92.         x = writemsg (MSGSIGNON);
  93.         if (x == SCMOK)  x = writeint (PROTOVERSION);
  94.         if (x == SCMOK)  x = writeint (pgmversion);
  95.         if (x == SCMOK)  x = writestring (scmversion);
  96.         if (x == SCMOK)  x = writemend ();
  97.     }
  98.     return (x);
  99. }
  100.  
  101. int msgsignonack ()
  102. {
  103.     register int x;
  104.  
  105.     if (server) {
  106.         x = writemsg (MSGSIGNONACK);
  107.         if (x == SCMOK)  x = writeint (PROTOVERSION);
  108.         if (x == SCMOK)  x = writeint (pgmversion);
  109.         if (x == SCMOK)  x = writestring (scmversion);
  110.         if (x == SCMOK)  x = writeint (fspid);
  111.         if (x == SCMOK)  x = writemend ();
  112.     } else {
  113.         x = readmsg (MSGSIGNONACK);
  114.         if (x == SCMOK)  x = readint (&protver);
  115.         if (x == SCMOK)  x = readint (&pgmver);
  116.         if (x == SCMOK)  x = readstring (&scmver);
  117.         if (x == SCMOK)  x = readint (&fspid);
  118.         if (x == SCMOK)  x = readmend ();
  119.     }
  120.     return (x);
  121. }
  122.  
  123. /*
  124.  * setup message
  125.  */
  126. extern int    xpatch;            /* setup crosspatch to a new client */
  127. extern char    *xuser;            /* user,group,acct for crosspatch */
  128. extern char    *collname;        /* base directory */
  129. extern char    *basedir;        /* base directory */
  130. extern int    basedev;        /* base directory device */
  131. extern int    baseino;        /* base directory inode */
  132. extern long    lasttime;        /* time of last upgrade */
  133. extern int    listonly;        /* only listing files, no data xfer */
  134. extern int    newonly;        /* only send new files */
  135. extern char    *release;        /* release name */
  136. extern int    setupack;        /* ack return value for setup */
  137.  
  138. int msgsetup ()
  139. {
  140.     register int x;
  141.  
  142.     if (server) {
  143.         x = readmsg (MSGSETUP);
  144.         if (x != SCMOK)  return (x);
  145.         if (protver >= 7) {
  146.             x = readint (&xpatch);
  147.             if (x != SCMOK)  return (x);
  148.         } else
  149.             xpatch = FALSE;
  150.         if (xpatch) {
  151.             x = readstring (&xuser);
  152.             if (x != SCMOK)  return (x);
  153.             return (readmend ());
  154.         }
  155.         x = readstring (&collname);
  156.         if (x == SCMOK)  x = readint ((int *)&lasttime);
  157.         if (x == SCMOK)  x = readstring (&basedir);
  158.         if (x == SCMOK)  x = readint (&basedev);
  159.         if (x == SCMOK)  x = readint (&baseino);
  160.         if (x == SCMOK)  x = readint (&listonly);
  161.         if (x == SCMOK)  x = readint (&newonly);
  162.         if (x == SCMOK)
  163.             if (protver < 6)
  164.                 release = (char *)NULL;
  165.             else
  166.                 x = readstring (&release);
  167.         if (x == SCMOK)  x = readmend ();
  168.     } else {
  169.         x = writemsg (MSGSETUP);
  170.         if (x != SCMOK)  return (x);
  171.         if (protver >= 7) {
  172.             x = writeint (xpatch);
  173.             if (x != SCMOK)  return (x);
  174.         }
  175.         if (xpatch) {
  176.             x = writestring (xuser);
  177.             if (x != SCMOK)  return (x);
  178.             return (writemend ());
  179.         }
  180.         if (x == SCMOK)  x = writestring (collname);
  181.         if (x == SCMOK)  x = writeint ((int)lasttime);
  182.         if (x == SCMOK)  x = writestring (basedir);
  183.         if (x == SCMOK)  x = writeint (basedev);
  184.         if (x == SCMOK)  x = writeint (baseino);
  185.         if (x == SCMOK)  x = writeint (listonly);
  186.         if (x == SCMOK)  x = writeint (newonly);
  187.         if (x == SCMOK && protver >= 6)  x = writestring (release);
  188.         if (x == SCMOK)  x = writemend ();
  189.     }
  190.     return (x);
  191. }
  192.  
  193. int msgsetupack ()
  194. {
  195.     if (server)
  196.         return (writemint (MSGSETUPACK,setupack));
  197.     return (readmint (MSGSETUPACK,&setupack));
  198. }
  199.  
  200. /*
  201.  * crypt test message
  202.  */
  203. extern char    *crypttest;        /* encryption test string */
  204.  
  205. int msgcrypt ()
  206. {
  207.     if (server)
  208.         return (readmstr (MSGCRYPT,&crypttest));
  209.     return (writemstr (MSGCRYPT,crypttest));
  210. }
  211.  
  212. int msgcryptok ()
  213. {
  214.     if (server)
  215.         return (writemnull (MSGCRYPTOK));
  216.     return (readmnull (MSGCRYPTOK));
  217. }
  218.  
  219. /*
  220.  * login message
  221.  */
  222. extern char    *logcrypt;        /* login encryption test */
  223. extern char    *loguser;        /* login username */
  224. extern char    *logpswd;        /* password for login */
  225. extern int    logack;            /* login ack status */
  226. extern char    *logerror;        /* error from login */
  227.  
  228. int msglogin ()
  229. {
  230.     register int x;
  231.     if (server) {
  232.         x = readmsg (MSGLOGIN);
  233.         if (x == SCMOK)  x = readstring (&logcrypt);
  234.         if (x == SCMOK)  x = readstring (&loguser);
  235.         if (x == SCMOK)  x = readstring (&logpswd);
  236.         if (x == SCMOK)  x = readmend ();
  237.     } else {
  238.         x = writemsg (MSGLOGIN);
  239.         if (x == SCMOK)  x = writestring (logcrypt);
  240.         if (x == SCMOK)  x = writestring (loguser);
  241.         if (x == SCMOK)  x = writestring (logpswd);
  242.         if (x == SCMOK)  x = writemend ();
  243.     }
  244.     return (x);
  245. }
  246.  
  247. int msglogack ()
  248. {
  249.     register int x;
  250.     if (server) {
  251.         x = writemsg (MSGLOGACK);
  252.         if (x == SCMOK)  x = writeint (logack);
  253.         if (x == SCMOK)  x = writestring (logerror);
  254.         if (x == SCMOK)  x = writemend ();
  255.     } else {
  256.         x = readmsg (MSGLOGACK);
  257.         if (x == SCMOK)  x = readint (&logack);
  258.         if (x == SCMOK)  x = readstring (&logerror);
  259.         if (x == SCMOK)  x = readmend ();
  260.     }
  261.     return (x);
  262. }
  263.  
  264. /*
  265.  * refuse list message
  266.  */
  267. extern TREE    *refuseT;        /* tree of files to refuse */
  268.  
  269. static int refuseone (t)
  270. register TREE *t;
  271. {
  272.     return (writestring (t->Tname));
  273. }
  274.  
  275. int msgrefuse ()
  276. {
  277.     register int x;
  278.     if (server) {
  279.         char *name;
  280.         x = readmsg (MSGREFUSE);
  281.         if (x == SCMOK)  x = readstring (&name);
  282.         while (x == SCMOK) {
  283.             if (name == NULL)  break;
  284.             (void) Tinsert (&refuseT,name,FALSE);
  285.             free (name);
  286.             x = readstring (&name);
  287.         }
  288.         if (x == SCMOK)  x = readmend ();
  289.     } else {
  290.         x = writemsg (MSGREFUSE);
  291.         if (x == SCMOK)  x = Tprocess (refuseT,refuseone);
  292.         if (x == SCMOK)  x = writestring ((char *)NULL);
  293.         if (x == SCMOK)  x = writemend ();
  294.     }
  295.     return (x);
  296. }
  297.  
  298. /*
  299.  * list files message
  300.  */
  301. extern TREE    *listT;            /* tree of files to list */
  302. extern long    scantime;        /* time that collection was scanned */
  303.  
  304. static int listone (t)
  305. register TREE *t;
  306. {
  307.     register int x;
  308.  
  309.     x = writestring (t->Tname);
  310.     if (x == SCMOK)  x = writeint ((int)t->Tmode);
  311.     if (x == SCMOK)  x = writeint ((int)t->Tflags);
  312.     if (x == SCMOK)  x = writeint (t->Tmtime);
  313.     return (x);
  314. }
  315.  
  316. int msglist ()
  317. {
  318.     register int x;
  319.     if (server) {
  320.         x = writemsg (MSGLIST);
  321.         if (x == SCMOK)  x = Tprocess (listT,listone);
  322.         if (x == SCMOK)  x = writestring ((char *)NULL);
  323.         if (x == SCMOK)  x = writeint ((int)scantime);
  324.         if (x == SCMOK)  x = writemend ();
  325.     } else {
  326.         char *name;
  327.         int mode,flags,mtime;
  328.         register TREE *t;
  329.         x = readmsg (MSGLIST);
  330.         if (x == SCMOK)  x = readstring (&name);
  331.         while (x == SCMOK) {
  332.             if (name == NULL)  break;
  333.             x = readint (&mode);
  334.             if (x == SCMOK)  x = readint (&flags);
  335.             if (x == SCMOK)  x = readint (&mtime);
  336.             if (x != SCMOK)  break;
  337.             t = Tinsert (&listT,name,TRUE);
  338.             free (name);
  339.             t->Tmode = mode;
  340.             t->Tflags = flags;
  341.             t->Tmtime = mtime;
  342.             x = readstring (&name);
  343.         }
  344.         if (x == SCMOK)  x = readint ((int *)&scantime);
  345.         if (x == SCMOK)  x = readmend ();
  346.     }
  347.     return (x);
  348. }
  349.  
  350. /*
  351.  * files needed message
  352.  */
  353. extern TREE    *needT;            /* tree of files to need */
  354.  
  355. static int needone (t)
  356. register TREE *t;
  357. {
  358.     register int x;
  359.     x = writestring (t->Tname);
  360.     if (x == SCMOK)  x = writeint ((t->Tflags&FUPDATE) != 0);
  361.     return (x);
  362. }
  363.  
  364. int msgneed ()
  365. {
  366.     register int x;
  367.     if (server) {
  368.         char *name;
  369.         int update;
  370.         register TREE *t;
  371.         x = readmsg (MSGNEED);
  372.         if (x == SCMOK)  x = readstring (&name);
  373.         while (x == SCMOK) {
  374.             if (name == NULL)  break;
  375.             x = readint (&update);
  376.             if (x != SCMOK)  break;
  377.             t = Tinsert (&needT,name,TRUE);
  378.             free (name);
  379.             if (update)  t->Tflags |= FUPDATE;
  380.             x = readstring (&name);
  381.         }
  382.         if (x == SCMOK)  x = readmend ();
  383.     } else {
  384.         x = writemsg (MSGNEED);
  385.         if (x == SCMOK)  x = Tprocess (needT,needone);
  386.         if (x == SCMOK)  x = writestring ((char *)NULL);
  387.         if (x == SCMOK)  x = writemend ();
  388.     }
  389.     return (x);
  390. }
  391.  
  392. /*
  393.  * files denied message
  394.  */
  395. extern TREE    *denyT;            /* tree of files to deny */
  396.  
  397. static int denyone (t)
  398. register TREE *t;
  399. {
  400.     return (writestring (t->Tname));
  401. }
  402.  
  403. int msgdeny ()
  404. {
  405.     register int x;
  406.     if (server) {
  407.         x = writemsg (MSGDENY);
  408.         if (x == SCMOK)  x = Tprocess (denyT,denyone);
  409.         if (x == SCMOK)  x = writestring ((char *)NULL);
  410.         if (x == SCMOK)  x = writemend ();
  411.     } else {
  412.         char *name;
  413.         x = readmsg (MSGDENY);
  414.         if (x == SCMOK)  x = readstring (&name);
  415.         while (x == SCMOK) {
  416.             if (name == NULL)  break;
  417.             (void) Tinsert (&denyT,name,FALSE);
  418.             free (name);
  419.             x = readstring (&name);
  420.         }
  421.         if (x == SCMOK)  x = readmend ();
  422.     }
  423.     return (x);
  424. }
  425.  
  426. /*
  427.  * send file message
  428.  */
  429. int msgsend ()
  430. {
  431.     if (server)
  432.         return (readmnull (MSGSEND));
  433.     return (writemnull (MSGSEND));
  434. }
  435.  
  436. /*
  437.  * receive file message
  438.  */
  439. extern TREE    *upgradeT;        /* pointer to file being upgraded */
  440.  
  441. static int writeone (t)
  442. register TREE *t;
  443. {
  444.     return (writestring (t->Tname));
  445. }
  446.  
  447.  
  448. #if __STDC__
  449. int msgrecv (PFI xferfile,...)
  450. #else
  451. /*VARARGS*//*ARGSUSED*/
  452. int msgrecv (va_alist)
  453. va_dcl
  454. #endif
  455. {
  456. #if !__STDC__
  457.     typedef int (*PFI)();
  458.     PFI xferfile;
  459. #endif
  460.     va_list args;
  461.     register int x;
  462.     register TREE *t = upgradeT;
  463. #if __STDC__
  464.     va_start(args,xferfile);
  465. #else
  466.     va_start(args);
  467.     xferfile = va_arg(args, PFI);
  468. #endif
  469.     if (server) {
  470.         x = writemsg (MSGRECV);
  471.         if (t == NULL) {
  472.             if (x == SCMOK)  x = writestring ((char *)NULL);
  473.             if (x == SCMOK)  x = writemend ();
  474.             return (x);
  475.         }
  476.         if (x == SCMOK)  x = writestring (t->Tname);
  477.         if (x == SCMOK)  x = writeint (t->Tmode);
  478.         if (t->Tmode == 0) {
  479.             if (x == SCMOK)  x = writemend ();
  480.             return (x);
  481.         }
  482.         if (x == SCMOK)  x = writeint (t->Tflags);
  483.         if (x == SCMOK)  x = writestring (t->Tuser);
  484.         if (x == SCMOK)  x = writestring (t->Tgroup);
  485.         if (x == SCMOK)  x = writeint (t->Tmtime);
  486.         if (x == SCMOK)  x = Tprocess (t->Tlink,writeone);
  487.         if (x == SCMOK)  x = writestring ((char *)NULL);
  488.         if (x == SCMOK)  x = Tprocess (t->Texec,writeone);
  489.         if (x == SCMOK)  x = writestring ((char *)NULL);
  490.         if (x == SCMOK)  x = (*xferfile) (t,args);
  491.         if (x == SCMOK)  x = writemend ();
  492.     } else {
  493.         char *linkname,*execcmd;
  494.         if (t == NULL)  return (SCMERR);
  495.         x = readmsg (MSGRECV);
  496.         if (x == SCMOK)  x = readstring (&t->Tname);
  497.         if (x == SCMOK && t->Tname == NULL) {
  498.             x = readmend ();
  499.             if (x == SCMOK)  x = (*xferfile) (NULL,args);
  500.             return (x);
  501.         }
  502.         if (x == SCMOK)  x = readint (&t->Tmode);
  503.         if (t->Tmode == 0) {
  504.             x = readmend ();
  505.             if (x == SCMOK)  x = (*xferfile) (t,args);
  506.             return (x);
  507.         }
  508.         if (x == SCMOK)  x = readint (&t->Tflags);
  509.         if (x == SCMOK)  x = readstring (&t->Tuser);
  510.         if (x == SCMOK)  x = readstring (&t->Tgroup);
  511.         if (x == SCMOK)  x = readint (&t->Tmtime);
  512.         t->Tlink = NULL;
  513.         if (x == SCMOK)  x = readstring (&linkname);
  514.         while (x == SCMOK) {
  515.             if (linkname == NULL)  break;
  516.             (void) Tinsert (&t->Tlink,linkname,FALSE);
  517.             free (linkname);
  518.             x = readstring (&linkname);
  519.         }
  520.         t->Texec = NULL;
  521.         if (x == SCMOK)  x = readstring (&execcmd);
  522.         while (x == SCMOK) {
  523.             if (execcmd == NULL)  break;
  524.             (void) Tinsert (&t->Texec,execcmd,FALSE);
  525.             free (execcmd);
  526.             x = readstring (&execcmd);
  527.         }
  528.         if (x == SCMOK)  x = (*xferfile) (t,args);
  529.         if (x == SCMOK)  x = readmend ();
  530.     }
  531.     va_end(args);
  532.     return (x);
  533. }
  534.  
  535. /*
  536.  * protocol done message
  537.  */
  538. extern int    doneack;
  539. extern char    *donereason;
  540.  
  541. int msgdone ()
  542. {
  543.     register int x;
  544.  
  545.     if (protver < 6) {
  546.         printf ("Error, msgdone should not have been called.");
  547.         return (SCMERR);
  548.     }
  549.     if (server) {
  550.         x = readmsg (MSGDONE);
  551.         if (x == SCMOK)  x = readint (&doneack);
  552.         if (x == SCMOK)  x = readstring (&donereason);
  553.         if (x == SCMOK)  x = readmend ();
  554.     } else {
  555.         x = writemsg (MSGDONE);
  556.         if (x == SCMOK)  x = writeint (doneack);
  557.         if (x == SCMOK)  x = writestring (donereason);
  558.         if (x == SCMOK)  x = writemend ();
  559.     }
  560.     return (x);
  561. }
  562.  
  563. /*
  564.  * go away message
  565.  */
  566. extern char    *goawayreason;        /* reason for goaway */
  567.  
  568. int msggoaway ()
  569. {
  570.     return (writemstr (MSGGOAWAY,goawayreason));
  571. }
  572.  
  573. /*
  574.  * cross-patch protocol message
  575.  */
  576. extern int    xargc;            /* arg count for crosspatch */
  577. extern char    **xargv;        /* arg array for crosspatch */
  578.  
  579. int msgxpatch ()
  580. {
  581.     register int x;
  582.     register int i;
  583.  
  584.     if (server) {
  585.         x = readmsg (MSGXPATCH);
  586.         if (x != SCMOK)  return (x);
  587.         x = readint (&xargc);
  588.         if (x != SCMOK)  return (x);
  589.         xargc += 2;
  590.         xargv = (char **)calloc (sizeof (char *),(unsigned)xargc+1);
  591.         if (xargv == NULL)
  592.             return (SCMERR);
  593.         for (i = 2; i < xargc; i++) {
  594.             x = readstring (&xargv[i]);
  595.             if (x != SCMOK)  return (x);
  596.         }
  597.         x = readmend ();
  598.     } else {
  599.         x = writemsg (MSGXPATCH);
  600.         if (x != SCMOK)  return (x);
  601.         x = writeint (xargc);
  602.         if (x != SCMOK)  return (x);
  603.         for (i = 0; i < xargc; i++) {
  604.             x = writestring (xargv[i]);
  605.             if (x != SCMOK)  return (x);
  606.         }
  607.         x = writemend ();
  608.     }
  609.     return (x);
  610. }
  611.  
  612. /*
  613.  * Compression check protocol message
  614.  */
  615. extern int    docompress;        /* Compress file before sending? */
  616.  
  617. int msgcompress ()
  618. {
  619.     if (server)
  620.         return (readmint (MSGCOMPRESS,&docompress));
  621.     return (writemint (MSGCOMPRESS, docompress));
  622. }
  623.